package dtaskboard.client.toolbar;

import java.util.Collection;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;

import dtaskboard.client.Constants;
import dtaskboard.client.dto.LoginDTO;
import dtaskboard.client.dto.RoleDTO;
import dtaskboard.client.login.LoginPage;
import dtaskboard.client.project.ProjectPage;
import dtaskboard.client.project.ProjectToolbar;
import dtaskboard.client.service.LoginService;
import dtaskboard.client.service.LoginServiceAsync;
import dtaskboard.client.util.OpenDialogCommand;


public class Toolbar {

  public Toolbar() {
    LoginServiceAsync loginService = GWT.create(LoginService.class);
    String sessionId = Cookies.getCookie(Constants.sessionIdCookie);
    loginService.getLoginDTO(sessionId, new AsyncCallback<LoginDTO>() {

      @Override
      public void onSuccess(LoginDTO dto) {
        String login = dto.getLogin();
        Collection<RoleDTO> roles = dto.getRoles();
        Panel root = RootPanel.get(Constants.toolbarPanel);
        MenuBar mainMenu = new MenuBar(false);
        mainMenu.setAnimationEnabled(true);

        MenuBar loginMenu = new MenuBar(true);
        loginMenu.setAnimationEnabled(true);
        mainMenu.addItem(login, loginMenu);

        loginMenu.addItem("Trocar senha", new OpenDialogCommand(
            new ChangePasswordDialog()));
        loginMenu.addItem("Logout", new LogoutCommand());


        MenuBar projectsMenu = new MenuBar(true);
        projectsMenu.setAnimationEnabled(true);
        mainMenu.addItem("Projetos", projectsMenu);

        Collection<String> projects = ToolbarUtil.ableProjects(roles);
        for (String projectName : projects) {
          projectsMenu
              .addItem(projectName, new OpenProjectCommand(projectName));
        }

        if (ToolbarUtil.checkAdminRole(roles)) {
          MenuBar adminMenu = new MenuBar(true);
          adminMenu.setAnimationEnabled(true);
          adminMenu.addItem("Criar usuário", new OpenDialogCommand(
              new CreateUserDialog()));
          adminMenu.addItem("Criar projeto", new OpenDialogCommand(
              new CreateProjectDialog()));
          adminMenu.addItem("Atribuir role", new OpenDialogCommand(
              new AddRoleToUserDialog()));
          mainMenu.addItem("Administração", adminMenu);
        }

        root.add(mainMenu);

      }

      @Override
      public void onFailure(Throwable caught) {
        new LoginPage();
      }
    });

  }
}


class OpenProjectCommand implements Command {
  private final String projectName;

  public OpenProjectCommand(String projectName) {
    this.projectName = projectName;
  }

  @Override
  public void execute() {
    new ProjectToolbar(this.projectName);
    new ProjectPage(this.projectName);
  }
}


class LogoutCommand implements Command {

  @Override
  public void execute() {
    String sessionId = Cookies.getCookie(Constants.sessionIdCookie);
    Cookies.removeCookie(Constants.sessionIdCookie);
    LoginServiceAsync loginService = GWT.create(LoginService.class);
    loginService.logout(sessionId, new AsyncCallback<Void>() {

      @Override
      public void onFailure(Throwable caught) {
        new LoginPage();
      }

      @Override
      public void onSuccess(Void result) {
        new LoginPage();
      }

    });
  }

}
